package com.sharemarking.application.common.service;


import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;


import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;




import com.sharemarking.application.common.entity.Eq;
import com.sharemarking.application.common.entity.Equitment;

import com.sharemarking.application.common.entity.Resource;

import com.sharemarking.application.common.entity.Room;

import com.sharemarking.application.common.repository.EmployeeRepo;
import com.sharemarking.application.common.repository.EquitmentRepo;
import com.sharemarking.application.common.repository.ResourceRepo;
import com.sharemarking.application.common.repository.RoomRepo;

@Controller
@RequestMapping("function")
public class PhoneEqController {
	
	
	@Autowired
	private RoomRepo dao1;
	@Autowired
	private EquitmentRepo dao2;
	@Autowired
	private ResourceRepo dao3;
	@Autowired
	private EmployeeRepo dao4;
	
	
	@RequestMapping(value = "findroom")
	@ResponseBody
	public List<Room> findroom(Room room){
		List<Room> list=dao1.findByemployee_idOrderByUpdateDateDesc(room.getCreateBy());
		return list;
	}
	
	@RequestMapping(value = "findequirment")
	@ResponseBody
	public List<Equitment> findequirment(Equitment equitment){
		List<Equitment> list=dao2.findByemployee_idOrderByUpdateDateDesc(equitment.getCreateBy());
		return list;
	}
	
	@RequestMapping(value = "insertroom")
	@ResponseBody
	public String inserteroom(Room room){
		room.setEmployee(dao4.findOne(room.getCreateBy()));
		room.setState("已审核");
		dao1.save(room);
		return "1";
	}
	
	@RequestMapping(value = "insertequitment")
	@ResponseBody
	public String insertequitment(Equitment equitment){
		equitment.setEmployee(dao4.findOne(equitment.getCreateBy()));
		equitment.setState("已审核");
		dao2.save(equitment);
		return "1";
	}

	@RequestMapping(value = "selectroom")
	@ResponseBody
	public List<Object> selectroom(Eq eq){
		Set<Object> set1=new HashSet<Object>();//存放全部会议室资源
		Set<Object> set2=new HashSet<Object>();//存放被申请的会议室资源
		
		//获取申请的开始时间与结束时间（开始时间不能比当前时间早）
		Date stime=eq.getStartTime();
		Date etime=eq.getEndTime();
		System.out.println(stime+";"+etime);
		//获取：全部的教室资源
		List list=(List)dao3.findAll();
		for(int i=0;i<list.size();i++){
			if(((Resource)list.get(i)).getType().equals("会议室")){
			set1.add(((Resource)list.get(i)).getName());
			}
		}
		//获取：此刻时间之前被申请的教室，已经在进行，但还没结束（开始时间<此刻的时间<结束时间）
		list=dao1.findByTime(new Date(),stime,etime,new Date());
		for(int i=0;i<list.size();i++){
			System.out.println(list.get(i));
			set2.add(list.get(i));
		}
		/*
		 获取：此刻时间之后被申请的教室，还没开始（此刻的时间<开始时间），同时前端申请的时间正与这区间有重叠（3种情况）
		1、（前端开始时间<开始时间<前端结束时间）或
		2、(开始时间<前端开始时间<前端结束时间<结束时间)或
		3、（前端开始时间<结束时间<前端结束时间）
		*/
		list=dao1.findByTime1(new Date(),stime,etime);
		for(int i=0;i<list.size();i++){
			System.out.println(list.get(i));
			set2.add( list.get(i));
		}
		Iterator<Object> it=set2.iterator();
		while(it.hasNext()){
			set1.remove(it.next());
		}
		List<Object> list1= new ArrayList<Object>(set1);
		return list1;
	}
	
	@RequestMapping(value = "selectequitment")
	@ResponseBody
	public List selectequitment(Eq eq) {
		
		Map<String, Integer> map1 = new HashMap<String, Integer>();
		String name = null; 
		Integer number = null;
		List list=null;
		List list1=new ArrayList<>();

		// 获取申请的开始时间与结束时间（开始时间不能比当前时间早）
		Date stime = eq.getStartTime();
		Date etime = eq.getEndTime();
		System.out.println(stime + ";" + etime);
		
		// 第一种情况
		list = dao2.findByTime(new Date(), stime, etime, new Date());
		for (int i = 0; i < list.size(); i++) {
			name = ((Equitment) list.get(i)).getName();
			number = ((Equitment) list.get(i)).getSum();
			map1.put(name, number);
		}
		// 第二种情况
		list = dao2.findByTime1(new Date(), stime, etime);
		for (int i = 0; i < list.size(); i++) {
			name = ((Equitment) list.get(i)).getName();
			number = ((Equitment) list.get(i)).getSum();
			if(map1.containsKey(name)){
				number=number+map1.get(name);
			}
			map1.put(name, number);
		}
		// 获取：全部空闲资源
		list = (List) dao3.findAll();
		for (int i = 0; i < list.size(); i++) {
			if ( ((Resource) list.get(i)).getType().equals("设备") ) {
				Eq eq1=new Eq();
				 name=((Resource) list.get(i)).getName(); 
				 number=((Resource) list.get(i)).getNumber();
				 //System.out.println(name+"d"+number);
				 if(map1.containsKey(name)){
					 number= number-(Integer) map1.get(name);
					 eq1.setName(name);
					 eq1.setSum(number);
					 //System.out.println("if里面"+name+""+number);
				 }else {
					 eq1.setName(name);
					 eq1.setSum(number);
					 //System.out.println("else里面"+name+""+number);
				 }
				 list1.add(eq1);
			}
		}
		return list1;
	}

}
